package com.itheima.health.jobs;

import com.itheima.health.common.RedisConst;
import com.itheima.health.utils.QiniuUtils;
import com.qiniu.common.QiniuException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;
@Slf4j
public class ClearImageJob {
    /**
     * 定义清理图片的任务
     */

    @Autowired
    private JedisPool jedisPool;

    public void clearImageJob(){
        log.info("clearImageJob......");
        //找出垃圾图片
        try (Jedis jedis=jedisPool.getResource() ){
            Set<String> pics = jedis.sdiff(RedisConst.SETMEAL_PIC_RESOURCES, RedisConst.SETMEAL_PIC_DB_RESOURCES);
            for (String pic : pics) {
                //删除
                QiniuUtils.deleteFileFromQiniu(pic);
                jedis.srem(RedisConst.SETMEAL_PIC_RESOURCES,pic);
            }

        } catch (RuntimeException|QiniuException e) {
            log.error("图片清理失败",e);
        }

    }
}
